<?php
namespace Home\Controller\PublicAction;
use Think\Controller;
/*
 * 系统通知控制器 2016.6.12
 * author:candy
 */
class NoticeController extends Controller {
    
    public function _empty($name){
        $this->display("Event:404");      
    }

    public function index(){
        $Notice = D('Notice');
        $notice_list = $Notice->where('send_id='.session('user.user_id'))->join('user S ON S.user_id = notice.send_id')->
        join('user R ON R.user_id = notice.rec_id','LEFT')->field('notice_id,R.name as rname,S.name as sname,content,notice.datetime,title')->select();
        $this->assign('noticelist',$notice_list);
    	$this->display('User:mailbox');
    }

    /***
	 * 6.12
	 * 私信添加和获取
     */
    public function notice(){
	    $Notice = D('Notice');
    	if(!IS_POST){
    		$notice_list = $Notice->where('send_id='.session('user.user_id'))->join('user S ON S.user_id = notice.send_id')->
    		join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->select();
    		$this->assign('list',$notice_list);
    		$this->display('Test:testnotice');
    	}else{
    		$Notice->create();
    		$Notice->send_id = session('user.user_id');
    		$Notice->type="notice";
    		if($Notice->add()){
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败');
    		}
    	}
    }

    public function message(){
    	$Notice = D('Notice');
    	if(!IS_POST){
    		$notice_list = $Notice->where('send_id='.session('user.user_id'))->join('user S ON S.user_id = notice.send_id')->
    		join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->select();
    		$this->assign('list',$notice_list);
    		$this->display('Test:testnotice');
    	}else{
    		$Notice->create();
    		$Notice->send_id = session('user.user_id');
    		$Notice->type="message";
    		if($Notice->add()){
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败');
    		}
    	}
    }

    public function submit(){
        $Notice = D('Notice');
        if(!IS_POST){
            $notice_list = $Notice->where('send_id='.session('user.user_id'))->join('user S ON S.user_id = notice.send_id')->
            join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->select();
            $this->assign('list',$notice_list);
            $this->display('Test:testnotice');
        }else{
            $Notice->create();
            $Notice->send_id = session('user.user_id');
            $Notice->type="notice";
            if($Notice->add()){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }

    /**
     *7.3 获取通知详情（$id 通知ID，uid 接受用户的id , sid发送用户的id）
        后期加个访问权限设置
     */
    public function getContent($id = null,$uid=null,$sid=null){
        $Notice = D('Notice');
        if($id){
            $content = $Notice->where('notice_id='.$id)->join('user S ON S.user_id = notice.send_id')->
                join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->find();
            echo  "<div class='content'>".htmlspecialchars_decode($content['content'])."</div>";
        }
        if($uid){
            //查看自己接收的
            $contentlist = $Notice->where('rec_id='.$uid)->join('user S ON S.user_id = notice.send_id')->
                join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->select();
            dump($contentlist);
        }        
        if($sid){
            //查看自己发送的
            $contentlist = $Notice->where('send_id='.$sid)->join('user S ON S.user_id = notice.send_id')->
                join('user R ON R.user_id = notice.rec_id','LEFT')->field('R.name as rname,S.name as sname,content,notice.datetime,title')->select();
            dump($contentlist);
        }
    }

    public function uploadImg(){
        date_default_timezone_set("Asia/chongqing");
        $config = array(
            "savePath" => "/Public/Uploads/notice/" ,             //存储文件夹
            "maxSize" => 3145728 ,                   //允许的文件最大尺寸，单位KB
            "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
        );
        //上传文件目录
        $Path = "./Public/Uploads/notice";
        //背景保存在临时目录中
        $config[ "savePath" ] = $Path;
        $up = new \Org\Util\Uploader( "upfile" , $config );
        $type = $_REQUEST['type'];
        $callback=$_GET['callback'];

        $info = $up->getFileInfo();
        /**
         * 返回数据
         */
        if($callback) {
            echo '<script>'.$callback.'('.json_encode($info).')</script>';
        } else {
            echo json_encode($info);
            $pic_list[] = array('pic_name'=>$info['name'],'pic_path'=>$info['url'],'type'=>'ue');
            $pic = M('Picture');
            $pic->addAll($pic_list);
        }
    }
}